
// set python environment with (python set exec /usr/local/bin/python3
args loopnum acc trait
*** CW Loop
use "Data/CivilWarIntemediateData.dta", clear
display "`acc'"
display "`loopnum'"
python:
from sfi import Data
import numpy as np
import pandas as pd
df = pd.DataFrame()
get_it = str("`trait'")
print(get_it)
X = np.array(Data.get("`trait'"))
df[get_it]  = X
acc2 = int(`acc')
rand_ind = df.sample(frac=(1 - acc2/100.0))
df['temp'] = df[get_it]
df.loc[rand_ind.index, 'temp'] += 1
df.loc[df.temp > 1, 'temp'] = 0
traitperturbed = df['temp'].values
Data.addVarByte('`trait'perturbed')
Data.store('`trait'perturbed', None, traitperturbed)
end


local trait "`trait'perturbed"   

preserve 

drop if year < 1850 | year > 1875

collapse (sum) patentcount (mean) `trait', by(nclassgoogle1 year)

tsset nclassgoogle1 year
tsfill, full

foreach var in `trait' patentcount {
	replace `var' = 0 if `var' == .
}

foreach var in `trait' {
	gen threeyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount)/(patentcount + L.patentcount + L2.patentcount)
}

foreach var in `trait' {
	gen fiveyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount)
}

foreach var in `trait' {
	gen fouryear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount)
}

foreach var in `trait' {
	gen sixyear`var' = (`var'*patentcount + L.`var'*L.patentcount + L2.`var'*L2.patentcount + L3.`var'*L3.patentcount + L4.`var'*L4.patentcount + L5.`var'*L5.patentcount)/(patentcount + L.patentcount + L2.patentcount + L3.patentcount + L4.patentcount + L5.patentcount)
}


drop if year < 1851


gen missingkeyvariable = fouryear`trait' == . if year >= 1855
egen anymissing = max(missingkeyvariable), by(nclassgoogle1)
drop if anymissing == 1

gen L5fouryear`trait' = L5.fouryear`trait'
gen D5fouryear`trait' = fouryear`trait' - L5fouryear`trait'
drop if year < 1855

synth fouryear`trait' fouryear`trait'(1861) fouryear`trait'(1860) fouryear`trait'(1859) fouryear`trait'(1858) fouryear`trait'(1857) fouryear`trait'(1856) fouryear`trait'(1855), trunit(623) trperiod(1862) keep("Data/perturbation_results_CW/SyntheticControlCWperturb`trait'`acc'_`loopnum'.dta") replace


   

use "Data/perturbation_results_CW/SyntheticControlCWperturb`trait'`acc'_`loopnum'.dta", clear

rename _time year
rename _Y_synthetic `trait'control
rename _Y_treated `trait'treatment
tsset year 

gen period = .
replace period = 0 if year >= 1855 & year <= 1861
replace period = 1 if year >= 1862 & year <= 1866

egen treatmentperiod0B = mean(`trait'treatment) if period == 0
egen treatmentperiod0 = mean(treatmentperiod0B) 
egen treatmentperiod1B = mean(`trait'treatment) if period == 1
egen treatmentperiod1 = mean(treatmentperiod1B) 

egen controlperiod0B = mean(`trait'control) if period == 0
egen controlperiod0 = mean(controlperiod0B) 
egen controlperiod1B = mean(`trait'control) if period == 1
egen controlperiod1 = mean(controlperiod1B) 

gen diffindiff = (treatmentperiod1 - treatmentperiod0) - (controlperiod1 - controlperiod0)
keep if year == 1860
keep diffindiff
gen true = 0


save "Data/perturbation_results_CW/SynthResultLeadingEdgeWWIperturb`trait'`acc'_`loopnum'.dta", replace
restore 



